﻿using PCTInterface.Common;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utility;

namespace PCTInterface
{
    public partial class FileUpload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<string> result = new List<string>();
                try
                {
                    if (Request.Files.Count > 0)
                    {
                        string nonstr = Request["nonstr"];
                        if (string.IsNullOrEmpty(nonstr))
                        {
                            throw new NullReferenceException("随机串未设置");

                        }

                        string compare_sign = Request.Headers["REQUEST_TOKEN"];
                        if (string.IsNullOrEmpty(compare_sign))
                        {
                            throw new Exception("比对密钥不存在");
                        }

                        string current_sign = Tools.GenerateSign(nonstr);
                        if (!compare_sign.Equals(current_sign))
                        {
                            throw new Exception("密钥校验错误");
                        }
                        
                        for (int i=0;i< Request.Files.Count;i++  ) {
                            HttpPostedFile fi = Request.Files[i];
                            string fileName = fi.FileName;
                            string dir = ConfigurationManager.AppSettings["FILE_UPLOAD_PATH"];
                            if (string.IsNullOrEmpty(dir)) {
                                dir = Path.Combine(Server.MapPath("~"),"FILE_UPLOAD_PATH");
                            }
                            dir = Path.Combine(dir, DateTime.Now.ToString("yyMMdd"));
                            if (!Directory.Exists(dir)) {
                                Directory.CreateDirectory(dir);
                            }
                            string fullPath = Path.Combine(dir, fileName);
                            if (File.Exists(fullPath))
                            {
                                fullPath = Path.Combine(dir,  Path.GetFileNameWithoutExtension(fileName) + "_" + DateTime.Now.ToString("HHmmssfff") + Path.GetExtension(fileName));
                            }
                            fi.SaveAs(fullPath);
                            result.Add(fullPath);
                        }
                        Response.Write(JSONHelper.Instance.JavaScriptConvertMachion(new { RETURN_CODE = 0,RETURN_RESULT= result }));

                    }

                }
                catch (Exception ex)
                {
                    foreach (string fi in result) {
                        System.IO.File.Delete(fi);
                    }

                    Logger.Instance.WriteEventLog("文件上传失败", ex.Message);
                    Response.Write(JSONHelper.Instance.JavaScriptConvertMachion(new { RETURN_CODE = 99 }));
                }
            }
        }
    }
}